package util;

/**
 * String utility functions
 * @author Sion (mostly from work)
 */
public class StringUtil {

    /** 
     * Platform independent newline character. 
     */
    public static final String NEW_LINE = System.getProperty("line.separator");
    
    /**
     * Whitespace / blank character.
     */
    public static final char WHITESPACE_CHAR = ' ';
	
    /**
     * Padds the string to the left with the given character for the specified length.
     * @param input
     * @param length
     * @param paddingChar
     * @return
     */
    public static String padLeft(String input, int length, char paddingChar) {
        return padWithChar(input, length, paddingChar, 1);
    }

    /**
     * Padds the string to the right with the given character for the specified length.
     * @param input
     * @param length
     * @param paddingChar
     * @return
     */
    public static String padRight(String input, int length, char paddingChar) {
        return padWithChar(input, length, paddingChar, 2);
    }
	
	/**
     * Padds the string to the right or to the left with the given character for the specified length.
     * @param input
     * @param length
     * @param paddingChar
     * @param mode: 1=left padding 2=right padding
     * @return
     */
    private static String padWithChar(String input, int length, char paddingChar, int mode) {
        if (input == null) {
            input = "";
        }
        StringBuilder sb = new StringBuilder(input);
        int index = 0;
        if (mode == 2) {
            index = sb.length();
        }
        while (sb.length() < length) {
            sb.insert(index, paddingChar);
            index++;
        }
        return sb.toString();
    }
	
}
